package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class Demo6 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();
        ) {
            Statement statement = connection.createStatement();
            //查看老师的名字和所带班级的名字
            String sql = "SELECT t.name,t.salary,t.title,c.name,c.floor " +
                         "FROM teacher t " +
                         "JOIN class c ON t.id = c.teacher_id ";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                String tName = rs.getString("t.name");
                int tSalary = rs.getInt("t.salary");
                String tTitle = rs.getString("t.title");
                String cName = rs.getString("c.name");
                int cFloor = rs.getInt("c.floor");
                System.out.println(tName+","+tSalary+","+tTitle+","+cName+","+cFloor);
            }
        } catch (SQLException e) {
            e.getStackTrace();
        }

    }
}
